<template>
  <div :class="['navbar', isH5 && 'navbar-h5']">
    <div v-if="isPC" class="header">
      <div class="user-info">
        <!-- <Avatar useSkeletonAnimation :url="avatar" /> -->
        <div class="user-info-main">
          <slot name="profile"></slot>
        </div>
      </div>
    </div>
    <div class="navbar-list">
      <div v-for="item in navBarListForShow" :key="item.name" :class="[
        'navbar-list-item',
        currentSelectedNav === item.name && 'navbar-list-item-selected',
      ]" :title="item.label" @click="switchNavBar(item.name)">
        <Icon :file="(currentSelectedNav === item.name && item.selectedIcon) ||
          (isPC && item.icon) ||
          (!isPC && item.h5Icon)
          " :width="isH5 ? '32px' : '32px'" :height="isH5 ? '32px' : '32px'">
          <span class="num" v-if="item.name == 'message' && chatDataStore.unReadNum > 0">{{ chatDataStore.unReadNum }}</span>
          <!-- <span class="num" v-if="item.name == 'relation' && chatDataStore.groupNum > 0">{{ chatDataStore.groupNum }}</span> -->
        </Icon>
        <div class="navbar-list-item-name">
          {{ TUITranslateService.t(`${item.label}`) }}<span v-if="item.name == 'relation'">（{{ chatDataStore.groupNum }}）</span>
        </div>
      </div>
    </div>
    <!-- <div v-if="isPC" class="footer">
      <div class="setting">
        <Icon :file="settingPNG" @click="toggleSetting" @mousedown.stop></Icon>
        <div v-if="isSettingShow" class="setting-more">
          <slot name="setting"></slot>
        </div>
      </div>
    </div> -->
  </div>
</template>
<script setup lang="ts">
import { useChatDataStore } from '@/store/chat';
import { IConversationModel, StoreName, TUIStore, TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { default as fawuH5SVG, default as fawuWebSVG } from "../assets/icon/fawu-real.svg";
import fawuSelectedSVG from "../assets/icon/fawu-selected.svg";
import messageH5SVG from "../assets/icon/message-real.svg";
import messageSelectedSVG from "../assets/icon/message-selected.svg";
import messageWebSVG from "../assets/icon/message.svg";
import profileSelectedSVG from "../assets/icon/profile-selected.svg";
import profileH5SVG from "../assets/icon/profile.svg";
import {
  computed,
  defineEmits,
  defineProps,
  onMounted,
  ref,
  watch,
  withDefaults,
} from "../TUIKit/adapter-vue";
import Icon from "../TUIKit/components/common/Icon.vue";
import { IUserProfile } from "../TUIKit/interface";
import { isH5, isPC } from "../TUIKit/utils/env";

const chatDataStore = useChatDataStore()

export interface INavBarItem {
  icon: any;
  h5Icon: any;
  selectedIcon: any;
  name: string;
  label: string;
}

const props = withDefaults(
  defineProps<{
    currentNavBar: string;
    isSettingShow: boolean;
  }>(),
  {
    currentNavBar: () => "message",
    isSettingShow: false,
  }
);

const emits = defineEmits(["update:currentNavBar", "update:isSettingShow"]);

const currentSettingStatus = ref<boolean>(false);
const currentSelectedNav = ref(props.currentNavBar);
const navBarListForShow = computed<Array<INavBarItem>>(() => {
  return isPC ? navBarList.filter((item: INavBarItem) => item.name !== "profile") : navBarList;
});
const avatar = ref<string>("");
const conversationList = ref<IConversationModel[]>([]);

TUIStore.watch(StoreName.USER, {
  userProfile: (userProfileData: IUserProfile) => {
    avatar.value = userProfileData?.avatar || "";
  },
});

const unReplyNum = ref(0);

onMounted(() => {
  TUIStore.watch(StoreName.CONV, {
    conversationList: onConversationListUpdated,
  });
});

watch(() => chatDataStore.unReadNum, () => {
  TUIStore.watch(StoreName.CONV, {
    conversationList: onConversationListUpdated,
  })
})

const navBarList: Array<INavBarItem> = [
  {
    icon: messageWebSVG,
    h5Icon: messageH5SVG,
    selectedIcon: messageSelectedSVG,
    name: "message",
    label: "会 话",
  },
  // {
  //   icon: noreplyWebSVG,
  //   h5Icon: noreplyH5SVG,
  //   selectedIcon: noreplySelectedSVG,
  //   name: "noreply",
  //   label: "未回复列表",
  // },
  {
    icon: fawuWebSVG,
    h5Icon: fawuH5SVG,
    selectedIcon: fawuSelectedSVG,
    name: "relation",
    label: "我的法务部",
  },
  // {
  //   icon: evaluateWebSVG,
  //   h5Icon: evaluateH5SVG,
  //   selectedIcon: evaluateSelectedSVG,
  //   name: "evaluate",
  //   label: "评价列表",
  // },
  // {
  //   icon: complaintWebSVG,
  //   h5Icon: complaintH5SVG,
  //   selectedIcon: complaintSelectedSVG,
  //   name: "complaint",
  //   label: "投诉列表",
  // },
  // {
  //   icon: workOrderWebSVG,
  //   h5Icon: workOrderH5SVG,
  //   selectedIcon: workOrderSelectedSVG,
  //   name: "workOrder",
  //   label: "工单列表",
  // },
  {
    icon: profileH5SVG,
    h5Icon: profileH5SVG,
    selectedIcon: profileSelectedSVG,
    name: "profile",
    label: "个人中心",
  },
];

function toggleSetting() {
  currentSettingStatus.value = !currentSettingStatus.value;
  emits("update:isSettingShow", currentSettingStatus.value);
}

function switchNavBar(name: string) {
  currentSelectedNav.value = name;
  emits("update:currentNavBar", name);
}

watch(
  () => props.currentNavBar,
  () => {
    currentSelectedNav.value = props.currentNavBar;
  },
  {
    immediate: true,
  }
);
watch(
  () => props.isSettingShow,
  () => {
    currentSettingStatus.value = props.isSettingShow;
  },
  {
    immediate: true,
  }
);

function onConversationListUpdated(list: IConversationModel[]) {
  conversationList.value = [];
  list.forEach((item: any) => {
    if (Number(item.lastMessage.fromAccount) > 0) {
      conversationList.value.push(item)
    }
  })
  unReplyNum.value = conversationList.value.length
}
</script>
<style scoped lang="scss">
.navbar {
  display: flex;
  flex-direction: column;
  background: #fff;
  user-select: none;

  .navbar-list {
    box-sizing: border-box;
    display: flex;
    justify-content: space-around;
    flex: 1;
    width: 100%;

    .navbar-list-item {
      padding: 13px;
      cursor: pointer;
    }

    .navbar-list-item-name{
      font-size: 14px;
      color: #999;
    }

    .navbar-list-item-selected {
      background: #fff;

      .navbar-list-item-name {
        color: #006eff;
      }
    }
  }

  .header,
  .footer {
    background-color: #f9fafc;
  }

  .header {
    .user-info {
      padding: 12px;
      position: relative;

      .avatar {
        width: 36px;
        height: 36px;
        border-radius: 5px;
      }

      &:hover {
        .user-info-main {
          display: block;
        }
      }

      .user-info-main {
        width: 100%;
      }
    }
  }

  .footer {
    .setting {
      cursor: pointer;
      padding: 12px;
      position: relative;
      flex: 1;

      .setting-more {
        background: #fff;
        box-shadow: 0 1px 10px 0 rgba(2, 16, 43, 0.15);
        min-width: 11.25rem;
        border-radius: 0 4px 4px 0;
        position: absolute;
        bottom: 10px;
        left: 100%;
        z-index: 9999;
        white-space: nowrap;
      }
    }
  }
}

.navbar-h5 {
  padding: 0px;
  flex-direction: row;
  flex: 1;
  box-shadow: 0 0 18px 0 #f0f6ff;
  border-radius: 9px;
  overflow: hidden;

  .navbar-list {
    flex-direction: row;
    background: #ebf0f6;
    justify-content: space-around;

    .navbar-list-item {
      flex: 1;
      padding: 10px;

      .navbar-list-item-name {
        text-align: center;
        padding-top: 4px;
        font-size: 12px;
        color: #a0a3a6;
      }
    }

    .navbar-list-item-selected {
      background-color: inherit;

      .navbar-list-item-name {
        color: #006eff;
      }
    }
  }
}

.num {
  position: absolute;
  display: inline-block;
  right: 0;
  top: -5px;
  min-width: 10px;
  width: fit-content;
  padding: 0 2.5px;
  height: 15px;
  font-size: 10px;
  text-align: center;
  line-height: 15px;
  border-radius: 7.5px;
  color: #fff;
  background-color: red;
}
</style>
